const config = require('./config')

const { StargateClient } = require('@cosmjs/stargate');

const Logger = require('./logger');

const rpc = process.argv[2];
const num = process.argv[3];
const logfile = process.argv[4];
console.log('rpc, num, logfile', rpc, num, logfile);
const logger = new Logger("press", logfile, logfile, "info");


function idleSleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

const main= async ()=>{
    const client = await StargateClient.connect(rpc);

    let oldTime = new Date().getTime();
    logger.info("oldTime: ", oldTime);

    try {
        while(1){
            let run_time = new Date().getTime();
            let height = await client.getHeight();
            logger.info("run time: ",run_time, ' height: ', height);
            let i = 0;
            while(i<num) {
                logger.info("run time: ",run_time, ' -- i ======= ', i);
                let chainID = await client.getChainId();
                logger.info("With client, chain id:",  chainID);
                let balance = await client.getAllBalances("kava1uzd4yqy5lv2y5jv6pfs8vfndjqefgw4z5mzaj0");// <-- replace with your generated address
                logger.info("With client: balance: ", balance);


                let block = await client.getBlock(height-i)
                logger.info("with client: get block: ",block.id);
                i++;
            }

            await idleSleep(1000)

        }



    }catch (e) {
        logger.error("e: ", e);

    }

    let newTime = new Date().getTime();
    logger.info("newTime ", newTime);
    let spendTime = newTime - oldTime;

    logger.info('spendTime: ', spendTime);
    logger.info('spendTime / 1000: ', spendTime/ 1000)
}
main();
